<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item label="">
        <el-input v-model="queryParams.username" @keyup.enter.native="handleQuery('1')" placeholder="请输入用户名称" />
      </el-form-item>
      <el-form-item label="">
        <el-input v-model="queryParams.nickname" @keyup.enter.native="handleQuery('1')" placeholder="请输入用户名称" />
      </el-form-item>
      <el-form-item label="">
        <el-input v-model="queryParams.phone" @keyup.enter.native="handleQuery('1')" placeholder="请输入手机号码" />
      </el-form-item>
      <el-form-item label="" style="width: 90px">
        <el-select v-model="queryParams.status" placeholder="状态">
          <el-option
            v-for="item in statusOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="">
        <el-date-picker
          v-model="dateRange"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="创建开始日期"
          end-placeholder="创建结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery('1')">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="handleQuery('reset')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb5">
      <el-col :span="1.5"><el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button></el-col>
      <el-col :span="1.5"><el-button type="success" :disabled="single" icon="el-icon-edit" @click="handleUpdate(null)">修改</el-button></el-col>
      <el-col :span="1.5"><el-button type="danger" :disabled="multiple" icon="el-icon-delete" @click="handleDelete(null)">删除</el-button></el-col>
      <el-col :span="1.5"><el-button type="primary" :disabled="single" icon="el-icon-edit" @click="handleExport">导出</el-button></el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
      @row-dblclick="openDetail(row)"
    >
      <el-table-column type="selection"/>
      <el-table-column type="index" label="序号" width="45" align="center" />
      <el-table-column prop="username" label="登录名" min-width="80px" />
      <el-table-column prop="nickname" label="用户昵称" />
      <el-table-column prop="phone" label="手机号" align="center" />
      <el-table-column prop="email" label="邮箱" min-width="120px" />
      <el-table-column prop="gender" label="性别" :formatter="genderFormat" min-width="50px" align="center" />
      <el-table-column prop="status" label="状态" min-width="50px" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-value="1" inactive-value="0" @change="handleStatusChange(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建日期" min-width="120px" align="center" />
      <el-table-column prop="remark" label="备注" min-width="120px" />
      <el-table-column label="操作" min-width="150px" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="mini" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="text" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-button>
          <el-popover :ref="scope.row.id" placement="top" width="160" style="margin-left:10px">
            <p>确定要重置密码吗？</p>
            <div>
              <el-button type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button type="primary" @click="handleResetPassword(scope.row)">确定</el-button>
            </div>
            <el-button slot="reference" type="text" icon="el-icon-key">重置密码</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="handleQuery" />

    <el-dialog :visible.sync="dialogVisible" :title="title" width="650px">
      <el-form ref="edit" :model="form" :label-width="formLabelWidth">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户登录名">
              <el-input v-model="form.username" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户名称">
              <el-input v-model="form.nickname" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号">
              <el-input v-model="form.phone" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱">
              <el-input v-model="form.email" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别">
              <el-select v-model="form.gender">
                <el-option v-for="item in genderOptions" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio v-for="item in statusOptions" :key="item.dictValue" :label="item.dictValue">{{ item.dictLabel }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="角色">
              <el-select v-model="form.roleIds" multiple>
                <el-option v-for="item in roleOptions" :key="item.id" :value="item.id" :label="item.roleName"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" rows="3" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { changeUserStatus, resetPwd, addUser, updateUser } from '@/api/system/user'
import { listRole } from '@/api/system/role'
import initData from '@/mixins/initData'

export default {
  mixins: [initData],
  data() {
    return {
      statusOptions: [],
      genderOptions: [],
      queryParams: {
        username: undefined,
        nickname: undefined,
        phone: undefined,
        status: undefined
      },
      formLabelWidth: '100px',
      formReset: {
        status: '1'
      },
      roleOptions: []
    }
  },
  mounted() {
    this.handleQuery()
    this.getDict('status_normal').then(res => {
      this.statusOptions = res.data
    })
    this.getDict('user_sex').then(res => {
      this.genderOptions = res.data
    })
  },
  methods: {
    beforeInit() {
      this.base = '/system/user'
      this.modalName = '用户'
    },
    // 修改
    handleStatusChange(row) {
      const text = row.status === '0' ? '停用' : '启用'
      this.$confirm('确认要' + text + '【' + row.nickname + '】用户吗？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return changeUserStatus(row.id, row.status)
      }).then(res => {
        console.log(res)
        this.msgSuccess(text + '成功')
      }).catch(() => {
        row.status = row.status === '0' ? '1' : '0'
      })
    },
    genderFormat(row, column) {
      return this.selectDictLabel(this.genderOptions, row.gender)
    },
    // 重置密码
    handleResetPassword(row) {
      resetPwd(row.id).then(res => {
        if (res.code === 200) {
          this.msgSuccess('重置用户【' + row.nickname + '】的密码成功')
        } else {
          this.msgError(res.message)
        }
        this.$refs[row.id].doClose()
      })
    },
    // 新增按钮
    handleAdd() {
      this.reset()
      this.getRoles()
      this.title = '新增' + this.modalName
      this.form = this.formReset
      this.dialogVisible = true
    },
    // 修改按钮
    handleUpdate(row) {
      this.getRoles()
      console.log(row)
      this.form = row || this.row
      this.dialogVisible = true
      this.title = '修改' + this.modalName
    },
    // 获取所有的角色列表
    getRoles() {
      listRole().then(res => {
        this.roleOptions = res.data
      })
    },
    submitForm() {
      this.$refs['edit'].validate(valid => {
        if (valid) {
          console.log(this.form)
          if (!this.form.id) {
            addUser(this.form)
          } else {
            updateUser(this.form)
          }
          this.dialogVisible = false
          this.handleQuery()
        }
      })
    },
    openDetail(row) {
      console.log(row)
    },
    handleExport() {
      console.log('正在开发中')
    }
  }
}

</script>

<style lang="scss" scoped>
  .el-form-item {
    margin-right :20px;
  }
</style>
